package com.hicoffee.bm.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hicoffee.bm.model.TransactionDetails;
import com.hicoffee.bm.service.TransactionDetailsService;
import com.hicoffee.bm.mapper.TransactionDetailsMapper;
import com.hicoffee.bm.utils.ResponseUtil;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.radarwin.framework.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.radarwin.framework.util.StringUtil;
import com.radarwin.framework.web.response.ResponseWrapper;

import java.util.HashMap;
import java.util.Map;

/**
 * auto generated by generation tools v1.0.0
 */
@Service
public class TransactionDetailsServiceImpl extends BaseService<TransactionDetails> implements TransactionDetailsService {

    @Autowired
    private TransactionDetailsMapper transactionDetailsMapper;


    @Override
    public ResponseWrapper getTransactionDetailsPageList(Map<String, Object> params) {
        // 设置分页参数
        String pageNum = StringUtil.getString(params.get("page"));
        String pageSize = StringUtil.getString(params.get("rows"));
        String queryAsJson = StringUtil.getString(params.get("queryParams"));
        if (StringUtil.isEmpty(pageNum) || StringUtil.isEmpty(pageSize)) {
            return ResponseUtil.errorResponse("必要分页参数不能为空");
        }
        Map<String, Object> queryParams = new HashMap<>();
        if (StringUtil.isNotEmpty(queryAsJson)) {
            queryParams = JsonUtil.jsonToMap(queryAsJson);
        }
        PageHelper.startPage(Integer.valueOf(pageNum), Integer.valueOf(pageSize));
        return ResponseUtil.successResponse("page", new PageInfo<>(transactionDetailsMapper.getTransactionDetailsPageList(queryParams)));
    }

    @Override
    public ResponseWrapper saveTransaction(String json) {
        JSONObject jsonObject = JSON.parseObject(json);
        TransactionDetails transactionDetails = new TransactionDetails();
        transactionDetails.setAmount(jsonObject.getString("amount"));
        transactionDetails.setMerchantId(jsonObject.getString("merchantId"));
        transactionDetails.setTerminalId(jsonObject.getString("terminalId"));
        transactionDetails.setPayChannel(jsonObject.getString("payChannel"));
        transactionDetails.setTransName(jsonObject.getString("transName"));
        transactionDetails.setReason(jsonObject.getString("reason"));
        transactionDetails.setCoffee(jsonObject.getString("coffee"));
        transactionDetails.setResult(jsonObject.getString("result"));
        transactionDetails.setCardNo(jsonObject.getString("cardNo"));
        transactionDetails.setDuration(jsonObject.getString("duration"));

        this.insert(transactionDetails);
        return ResponseUtil.successResponse("success");
    }


}
